<?php
error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai');
define('API_URL', 'https://slog.slightech.com/report_data/add');
define('GET_KEY_URL', 'https://slog.slightech.com/report_item/get_item_key');
define('GET_SERVER_URL', 'https://slog.slightech.com/report_item/get_item_server');
require_once 'Base_fetch.php';
require_once 'Base_nginx_log.php';

function generate_buy_log() { 
	$item_server = Base_fetch::http_get(GET_SERVER_URL);
	$item_keys = Base_fetch::http_post(GET_KEY_URL, array('item_type' => 'buy_mynt', 'item_server' => $item_server));
	$key_arr = explode(" ", $item_keys);
	$buy_log = array();
	foreach ($key_arr as $k => $row) {
		$buy_key   = $row;
		$buy_value = substr($row, 9,strlen($row));
		$buy_log["$buy_key"] = "grep 'GET /buy/mynt' | grep ch={$buy_value}";
	}
	return $buy_log;

}

function generate_download_log() { 
	$item_server = Base_fetch::http_get(GET_SERVER_URL);
	$item_keys = Base_fetch::http_post(GET_KEY_URL, array('item_type' => 'download', 'item_server' => $item_server));
	$key_arr = explode(" ", $item_keys);
	$buy_log = array();
	foreach ($key_arr as $k => $row) {
		$download_key   = $row;
		$download_value = substr($row, 9,strlen($row));
		$download_log["$download_key"] = "grep 'GET /download/$download_value'";
	}
	return $download_log;
}

send_log(array_merge(generate_buy_log(), generate_download_log()));

function send_log($log_list) {
	$nginx = new Base_nginx_log();
	foreach ($log_list as $key => $grep) {
		$value = $nginx->grep($grep);
		$token = Base_fetch::generate_token($key, $value, date('Y-m-d'));
		Base_fetch::http_post(API_URL, array('key' => $key, 'value' => $value,'token' => $token));
	}
}
